//
//  ViewController.m
//  collectionView的轮播图
//
//  Created by mac on 2017/4/6.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"
#import "BrowsingCollectionViewCell.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#define itemW (kScreenWidth - 2 * 10)
#define itemH (kScreenHeight - 2 * 100)

#define sectionCount 10

#define BrowsingCell @"BrowsingCollectionViewCell.h"

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *myCollectionView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建数据源
    [self createData];
    
    //创建collectionView
    [self createCollectionView];
    
    //注册
    [self.myCollectionView registerClass:[BrowsingCollectionViewCell class] forCellWithReuseIdentifier:BrowsingCell];
    
    
    [self createPageControl];
    
    [self addNSTimer];
}


- (void)createPageControl{

    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.frame = CGRectMake(10, kScreenHeight - 80, itemW, 40);
    pageControl.currentPage = 0;
//    pageControl.backgroundColor = [UIColor lightGrayColor];
    pageControl.numberOfPages = 6;
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    
    [self.view addSubview:pageControl];
    
    self.pageControl = pageControl;
    

}

-(void)addNSTimer
{

   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
    //添加到runloop中
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    
    self.timer = timer;
}

-(void)nextPage
{
    NSLog(@"&&&&:%@",[self.myCollectionView indexPathsForVisibleItems]);
    
    //1）获取当前正在展示的位置
    NSIndexPath *currentIndexPath=[[self.myCollectionView indexPathsForVisibleItems]lastObject];
    
    //马上显示回最中间那组的数据
    NSIndexPath *currentIndexPathRest=[NSIndexPath indexPathForItem:currentIndexPath.item inSection:sectionCount/2];
    [self.myCollectionView scrollToItemAtIndexPath:currentIndexPathRest atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    　  //2）计算出下一个需要展示的位置
    NSInteger nextItem = currentIndexPath.item+1;
    NSInteger nextSection = currentIndexPathRest.section;

    if (nextItem == self.dataSource.count) {
            nextItem=0;
            nextSection++;
        }
    

    NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    
    　　//3）通过动画滚动到下一个位置
    [self.myCollectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    //4)设置页码
    self.pageControl.currentPage=nextItem;
}

- (void)createData{
    self.dataSource = [[NSMutableArray alloc] init];
        for (int i = 1 ; i <= 6 ; i ++) {
            NSString *imageName = [NSString stringWithFormat:@"%d.jpg",i];
            [_dataSource addObject:imageName];
        }
}

- (void)createCollectionView{
    
    //网格布局类
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumLineSpacing = 0.00001;
    layout.minimumInteritemSpacing = 0.000001;
    layout.itemSize = CGSizeMake(itemW, itemH);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 100, itemW, itemH) collectionViewLayout:layout];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.pagingEnabled = YES;
    [self.view addSubview:self.myCollectionView];

}

#pragma mark - UICollectionViewDataSource
//一共多少组，默认1组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return sectionCount;
}

//一组多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataSource.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BrowsingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:BrowsingCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    cell.icon = self.dataSource[indexPath.item];

    NSLog(@"***%@", cell);
    
    return cell;
}

#pragma mark-UICollectionViewDelegate
//当用户开始拖拽的时候就调用，取消定时器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeNSTimer];
}
//当用户停止拖拽的时候调用，重启定时器
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addNSTimer];
}
//设置页码
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page=(int)(scrollView.contentOffset.x/scrollView.frame.size.width+0.5)%self.dataSource.count;
    self.pageControl.currentPage=page;
}

- (void)removeNSTimer{
    [self.timer invalidate];
    self.timer = nil;
}

@end
